package org.easymis.crm.modules.crm.controller;

import java.util.List;

import org.easymis.commons.web.BaseController;
import org.easymis.commons.web.utils.Page;
import org.easymis.commons.web.utils.R;
import org.easymis.crm.modules.crm.entitys.mybatis.dto.CrmBusiness;
import org.easymis.crm.modules.crm.service.CrmBusinessService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(tags = "商机管理")
@Controller
public class CrmBusinessController extends BaseController {

	@Autowired
	CrmBusinessService service;

	@ApiOperation(value = "商机管理首页", notes = "JSON ", produces = MediaType.APPLICATION_JSON_VALUE)
	@RequestMapping(value = { "/crm/business/index.html" }, method = { RequestMethod.GET, RequestMethod.POST })
	public String index(@ModelAttribute Page page, ModelMap map) throws Exception {
		return getTplPath(map, "crm/business/index");
	}

	@ApiOperation(value = "商机分页列表", notes = "html", produces = MediaType.APPLICATION_JSON_VALUE)
	@RequestMapping(value = { "/crm/business/list.html" }, method = { RequestMethod.GET, RequestMethod.POST })
	public String list(@ModelAttribute Page page, ModelMap map) throws Exception {
		CrmBusiness bean = new CrmBusiness();
		service.getList(bean);
		return getTplPath(map, "crm/business/list");
	}

	@ApiOperation(value = "商机分页列表", notes = "JSON ", produces = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	@RequestMapping(value = { "/crm/business/list.json" }, method = { RequestMethod.GET, RequestMethod.POST })
	public R listJson(@ModelAttribute Page page, ModelMap map) throws Exception {
		PageHelper.startPage(page.getCurPage(), page.getPageSize());
		List businessList = service.getList(new CrmBusiness());
		PageInfo<CrmBusiness> p = new PageInfo<CrmBusiness>(businessList);
		return new R().put("pageInfo", p);
	}

	@ApiOperation(value = "添加", notes = "html", produces = MediaType.APPLICATION_JSON_VALUE)
	@RequestMapping(value = { "/crm/business/add.html" }, method = { RequestMethod.GET, RequestMethod.POST })
	public String forwardAdd(@ModelAttribute Page page, ModelMap map) throws Exception {

		return getTplPath(map, "crm/business/add");
	}

	@ApiOperation(value = "添加", notes = "JSON ", produces = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	@RequestMapping(value = { "/crm/business/save.do" }, method = { RequestMethod.GET, RequestMethod.POST })
	public R save(@ModelAttribute Page page, ModelMap map, CrmBusiness bean) throws Exception {
		service.save(bean);
		return new R().ok();
	}

	@ApiOperation(value = "删除", notes = "JSON ", produces = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	@RequestMapping(value = { "/crm/business/delete.do" }, method = { RequestMethod.GET, RequestMethod.POST })
	public R delete(@ModelAttribute Page page, ModelMap map, String businessId) throws Exception {
		service.delete(businessId);
		return new R().ok();
	}

	@ApiOperation(value = "编辑", notes = "html", produces = MediaType.APPLICATION_JSON_VALUE)
	@RequestMapping(value = { "/crm/business/edit.html" }, method = { RequestMethod.GET, RequestMethod.POST })
	public String forwardEdit(@ModelAttribute Page page, ModelMap map, String businessId) throws Exception {
		service.findById(businessId);
		return getTplPath(map, "crm/business/edit");
	}

	@ApiOperation(value = "修改", notes = "JSON ", produces = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	@RequestMapping(value = { "/crm/business/update.do" }, method = { RequestMethod.GET, RequestMethod.POST })
	public R update(@ModelAttribute Page page, ModelMap map, CrmBusiness bean) throws Exception {
		service.update(bean);
		return new R().ok();
	}

	@ApiOperation(value = "详情", notes = "JSON ", produces = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	@RequestMapping(value = { "/crm/business/view.json" }, method = { RequestMethod.GET, RequestMethod.POST })
	public R viewJson(@ModelAttribute Page page, ModelMap map, String businessId) throws Exception {
		return new R().put("business", service.findById(businessId));
	}

	@ApiOperation(value = "详情", notes = "html", produces = MediaType.APPLICATION_JSON_VALUE)
	@RequestMapping(value = { "/crm/business/view.html" }, method = { RequestMethod.GET, RequestMethod.POST })
	public String view(@ModelAttribute Page page, ModelMap map, String businessId) throws Exception {
		service.findById(businessId);
		return getTplPath(map, "crm/business/view");
	}

}
